<%@page import="Model.User"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String path_admin = "/SHOPCH4/Admin";
String path = "/SHOPCH4";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Admin Shop CH4</title>
        <link href="<%=path_admin %>/960.css" rel="stylesheet" type="text/css" media="all" />
        <link href="<%=path_admin %>/text.css" rel="stylesheet" type="text/css" media="all" />
        <link href="<%=path_admin %>/style.css" rel="stylesheet" type="text/css" media="all" />

        <style type="text/css">
            th{
                font-size: 15px;
                font-weight: bold;
            }

        </style>
        <script type="text/javascript">
            function areYouSure(){
                var sure = confirm("Are you sure?");
                if(sure){
                    return true;
                }else{
                    return false;
                }
            }
        </script>
       
    </head>
    <%
                   // String keyword = request.getParameter("username");
                    //keyword = (keyword == null) ? "" : keyword;

                   /* int numOfPage = 1;
                    try {
                        numOfPage = (Integer) request.getAttribute("numOfPage");
                    } catch (Exception ex) {
                        int numOfRecord = 1;
                        numOfPage = (numOfRecord % 15 == 0) ? numOfRecord / 15 : numOfRecord / 15 + 1;
                    }
                    */

                    
                   /* int listSize = 0;
                    if (listUser == null) {
                        listUser = User.searchUser("", 1);
                        listSize = listUser.size();
                    } else {
                        listSize = listUser.size();
                    }
                    */

        %>

    <body>
        <div class="container_16" id="content">
            <div class="" id="header">
                <div class="grid_4 alpha" id="admin"> </div>
                <div class="grid_12 omega" id="menu">
                    <jsp:include page="Navigation.jsp" flush="true">
                        <jsp:param name="accountManage" value="current"/>
                    </jsp:include>
                </div> <!-- End #menu -->
              <a style="color: red" href="../logout">Thoát</a>
               
            </div> <!-- End #header -->


            <div class="grid_16 alpha omega" id="mainContent">
                <div class="grid_4 alpha" id="leftPanel">
                    <h1>Tìm kiếm</h1>
                    <form action="<%=path %>/QuanLyNguoiDung">                    
                        UserName: <input type="text" name="username">
                        <input type="submit" value="Tìm">
                        <input type="hidden" name="action" value="searchUser">
                    </form><br/>

                    <div align="center" style="margin-top: 50px;">
                        <a href="AddUser.jsp" id="addNew"></a> <br/>
                        <h3 style="margin-top: 0px;">Thêm Người dùng</h3>
                    </div>
                </div> <!-- End #leftPanel -->

                <div class="grid_12 omega" id="mainPanel">
                    <h1>Danh sách Users</h1>
                    <table>
                        <tr>
                            <th width="20%">UserName</th>
                            <th width="25%">Email</th>
                            <th width="15%">Quyền</th>
                            <th width="10%"> Xóa User</th>
                            <th width="10%"> Xem Chi Tiết User</th>
                        </tr>

                        <%
                        int numRecord = 5; // page hien thi bao nhieu dong
                        int numOfRecord = 0; // tong so user
                        ArrayList<User> arr = null;
                        String url_action = "action=manageUser";
                        
                        if(request.getAttribute("listResult") !=null){
                        	arr= (ArrayList<User>) request.getAttribute("listResult");
                        	numOfRecord = User.countUser();
                        } 
                        
                        else if(request.getAttribute("listResultSerch") != null){
                        	arr= (ArrayList<User>) request.getAttribute("listResultSerch");
                        	String username = (String) request.getAttribute("searchUsername");
                    		numOfRecord = User.countUserSearch(username);
                    		out.println("KQ: "+numOfRecord+", "+username);
                        	url_action = "action=searchUser&username="+username;
                        }
                        
                        else{
	                        arr = User.getAllUser(numRecord, 1);
	                        numOfRecord = User.countUser();
                        }
                        
							for (int i = 0; i < arr.size(); i++) {
                                 User user1 = arr.get(i);
                        %>
                        <tr>
                            <td valign="middle" align="center"><%=user1.getUsername()%></td>
                            <td valign="middle" align="center"><%=user1.getEmail()%></td>
                            <td valign="middle" align="center"><%=user1.getQuyen().getTenQuyen()%></td>
                            <td valign="middle" align="center">
                                <form action="<%=path %>/QuanLyNguoiDung" method="post">
                                    <input type="submit" value="Xóa" onclick="return areYouSure()">
                                    <input type="hidden" name="action"  value="deleteUser">                                    
                                    <input type="hidden" value="<%=user1.getUserID()%>" name="UserID">
                                </form>
                            </td>
                            <td>
                                <form action="<%=path %>/QuanLyNguoiDung" method="post">
                                    <input type="submit" value="Xem chi tiết">
                                    <input type="hidden" name="action"  value="viewUser">
                                    <input type="hidden" value="<%=user1.getUserID()%>" name="UserID">
                                </form>
                            </td>
                        </tr>
                        <%} %>
                    </table>

                    <div class="grid_4 prefix_8" id="paging">
                    <%
                    		int numOfPage = (numOfRecord % numRecord == 0) ? numOfRecord / numRecord : numOfRecord / numRecord + 1;
                            for (int i = 1; i <= numOfPage; i++) {
                    %>
                        |<a href="/SHOPCH4/QuanLyNguoiDung?<%=url_action%>&page=<%=i%>"> <%=i%> </a>|
                    <%}%>
                    </div> <!-- End #paging -->
                </div> <!-- End #mainPanel -->
            </div> <!-- End #mainContent -->
            <div id="footer" align="center"><font color="white">© 2013 Shop CH4</font></div>
        </div>
    </body>
</html>
